  /*
	@Author: 于辉
	@Time: 2020-11
	@Tittle: rolePowers
	@Description: 角色授权管理
*/
var vue , form  ,sourceData ,pageData,getUserByRoleidMethod;

 layui.use(["form", 'jquery',"table"],function(){
 	form = layui.form
	,table = layui.table;;
 	
 	$("#roleList").height($(window).height()-100);
 	$(window).resize(function(){
		$("#roleList").height($(window).height()-100);
	})
 	vue = new Vue({
 		el:"#powers",
 		data:{
 			selectRoleId:'',
 			search:'',
 			rolelist:[],
			userlist:[]
 		},
 		watch:{
 			// search:function (newValue, oldValue){
 			// 	var powersShowList = [];
 			// 	vue.powersList.forEach(v => {
 			// 		if(newValue == '' || v.page.indexOf(newValue)>-1){
				// 		powersShowList.push(v);
				// 	}
				// });
				// vue.powersShowList = powersShowList;
 			// }
 		},
 		updated:function(){
		    form.render();
		},
		methods:{
			changeRole:function(roleid){
				vue.selectRoleId = roleid;
                getUserByRoleid(roleid);
			},
		}
 	})
 	//获取所有角色列表
 	getRoleList();

     /*
      * 获取所有角色列表
      * */
     function getRoleList(){
         ajaxSetting({
             url:getServerPath("GETROLEMODEL"),
             type:"get",
             data:{id:''},
             success:function(res){
                 vue.rolelist = res.data;
                 if(vue.rolelist.length>0){
                     vue.selectRoleId = vue.rolelist[0].id;
                     getUserByRoleid(vue.rolelist[0].id)
                 }

             }
         })
     }

     //根据roleid获取该角色下所有授权的用户列表
     function getUserByRoleid(roleid){
			ajaxSetting({
				url:getServerPath("GETROLEUSERS"),
				type:'get',
				data:{roleid:roleid},
				success:function(res){
	                sourceData = res.data;
	                pageData = res.data;
	                let new_data = $.extend(true, [], res.data);
	                tableIns=table.render({
	                    elem: '#dataTable'
	                    , id: 'userID'
                        ,even: true //开启隔行背景
	                    , loading: true
	                    ,height: 'full-150'
	                    , autoSort: false //手动排序
	                    , page: true
	                    , limit: 10
	                    , limits:[50,100,200,300]
	                    , cols: [[
	                        {type:'checkbox',width:80}
	                        ,{field:'userID', title: 'ID',hide:true, sort: true}
	                        ,{field:'userName', width:200, title: '用户名',align:"center"}
	                        ,{field:'nickName', width:200, title: '昵称',align:"center"}
	                        ,{field:'phone', width:150, title: '手机号',align:"center"}
	                        ,{field:'sex', width:80, title: '性别',align:"center"}
	                        ,{field:'createTime', width:150, title: '创建时间',align:"center",templet:function(row){
	                                return new Date(row.createTime).Format("yyyy-MM-dd");
	                            }}
	                        ,{field:'enabled', width:150, title: '是否启用',align:"center",templet:function(row){
	                                if(row.enabled){
	                                    return "启用";
	                                }else{
	                                    return "禁用";
	                                }
	
	                            }}
	                        ,{field:'remark',  title: '备注',align:"center",templet:function(row){
	                                if(row.remark){
	                                    return "<div class='align-left'>"+row.remark+"</div>";
	                                }
	                                return "";
	                            }}
	                    ]]
	                    , data:new_data
	                    , done: function () {
	                        searchPage();
	                    }
	                });
                    $('#page').css('text-align', 'right');  // 分页右对齐
                    $('.layui-table-page').css('text-align', 'right');  // 数据表格分页右对齐
				}
	
			})
	 }
	//前端分页
     function searchPage() {
         $("#search_input").keydown(function (e) {
             var curKey = e.which;
             if (curKey == 13) {
                 var loading = layer.load(2, {
                     content:'搜索中...',
                     shade:  [0.3, '#393D49'],time: 3 * 1000
                 });
                 var Keyword = $('#search_input').val();
                 Keyword = trim(Keyword);
                 pageData = array_search(sourceData, Keyword);
                 let new_data = $.extend(true, [], pageData);
                 table.reload('dataTable', { data: new_data });
                 layer.close(loading);
                 $('#search_input').val(Keyword);
             }
         });
     }
 		$("#btn_addUser").click(function(){
 			layer.open({
 				type: 2,
			  title: "添加用户",
			  closeBtn: 1,
			  shadeClose: true,
			  area: ['803px', '600px'],
			  content: '../user/selectUsers.html?type=roleUser&mainID='+vue.selectRoleId
 			})
 		});
 		$("#btn_removeUser").click(function(){
 			var checkStatus = table.checkStatus('userID'); 
		 	if(checkStatus.data.length<=0){
		 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
		 		return;
		 	}
		 	var selectIds = new Array();
		 	for(var i=0;i<checkStatus.data.length;i++){
		 		selectIds.push(checkStatus.data[i].userID);
		 	}
		 	ajaxSetting({
		 		url:getServerPath("DELETEROLEUSER"),
		 		data:JSON.stringify({roleId:vue.selectRoleId,userIds:selectIds}),
		 		success:function(){
		 			layer.msg("删除用户成功",{icon:6});
		 			getUserByRoleidMethod();
		 		}
		 	})
 		})
 		getUserByRoleidMethod = function(){
 			getUserByRoleid(vue.selectRoleId);
 		}
 		
 });


  function reloadData(){
		getUserByRoleidMethod();
	}

